package com.jy.wms.controller;

import com.alibaba.fastjson.JSONArray;

import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.SysMenu;
import com.jy.wms.pojo.SysUser;
import com.jy.wms.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SysMenuController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date Tue Aug 29 15:29:31 GMT+08:00 2017
 */
/*
@Controller
@RequestMapping("/sysMenu")
@Api(value = "/sysMenu", description = "关于菜单类型操作")
*/


@RestController
@RequestMapping("/sysMenu")
@Api(value = "/sysMenu", description = "关于菜单类型操作")
public class SysMenuController extends BaseController<SysMenuService, SysMenu> {
    // private static Logger log = LoggerFactory.getLogger(LoginController.class);


    //获取所有菜单

    @ApiOperation(value = "获取所有菜单", response = JSONObject.class, notes = "获取所有菜单")
    @PostMapping(value = "/getMenu")
    public @ResponseBody
    JSONArray getMenus(SysUser user, HttpSession session) {
        Integer[] rid = new Integer[1];
        rid[0] = 9;
        JSONArray jsonArray = baseService.getMenu(rid );

        return jsonArray;
    }

    /**
     * 获取所有菜单 lh
     *
     * @return
     */

    @ApiOperation(value = "获取所有菜单lh", response = JSONObject.class, notes = "获取所有菜单lh")
    @PostMapping(value = "/getMenus")
    public @ResponseBody
    JSONArray getMenu() {
        JSONArray jsonArray = baseService.getMenus();
        return jsonArray;
    }

    /**
     * 增加节点信息
     *
     * @param menu
     * @return
     */


    @ApiOperation(value = "增加菜单信息", response = JSONObject.class, notes = "增加菜单信息")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject addMenu(@RequestBody SysMenu menu) {
        try {
            JSONObject json = getJSONObject();
            menu.setId(UUIDUtils.uuid());
            Integer a = baseService.getCountByCode(menu.getCode(), menu.getMc(),"");
            if (a == 0) {//判断数据库里是否有相同编号的菜单（不允许有相同编码的菜单）
                if (menu.getFjd() == null || menu.getFjd() == "") {
                    menu.setFjd("0");
                }
                baseService.baseInsert(menu);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"增加菜单成功","", JSONObject.fromObject(menu).toString());
            } else {
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "增加菜单失败", "菜单项编码或名称重复，增加失败", "");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "增加菜单失败", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"增加菜单成功",e.getMessage(), JSONObject.fromObject(menu).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "增加菜单失败", e.getMessage(), "");
        }
    }


    /**
     * 修改菜单
     *
     * @param entity
     * @return
     */
    @ApiOperation(value = "修改菜单信息", response = JSONObject.class, notes = "修改菜单信息")
    @PostMapping(value = "/edit")
    public @ResponseBody
    JSONObject editSysMenu(@RequestBody SysMenu entity) {
        try {
            JSONObject json = getJSONObject();
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改菜单成功","", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "修改菜单失败", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改菜单成功",e.getMessage(), JSONObject.fromObject(entity).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "修改菜单失败", e.getMessage(), "");

        }


    }

    /**
     * 删除菜单
     *
     * @param ids
     * @return
     */


    @ApiOperation(value = "删除菜单信息", response = JSONObject.class, notes = "删除菜单信息")
    @PostMapping(value = "/delete")
    public @ResponseBody
    JSONObject deleteSysMenu(@RequestBody List<String> ids) {
        try {
            JSONObject json = getJSONObject();
            Integer flag = baseService.baseDeleteBathPrimaryKeys(ids);
            if (flag == 0) {
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "删除菜单失败", "", "");
            } else {
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"删除菜单成功","", "");

            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "删除菜单失败", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "调整菜单顺序", notes = "{'id':菜单id,'showOrder':位置}")
    @PostMapping(value = "/updateWz")
    public @ResponseBody
    JSONObject updateOrder(@RequestBody Map map) {
        try {
            JSONObject json = getJSONObject();

            baseService.updateWz(map);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"菜单顺序调整成功","", "");

        }catch (ServiceException e) {

            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "菜单顺序调整失败", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"菜单顺序调整成功",e.getMessage(), "");

        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "菜单顺序调整失败", e.getMessage(), "");
        }
    }

}
